#include <QtGui>
#include "masterconfigform.h"
#include "valueform.h"
#include "valuesettingsform.h"
#include "../core/measprog.h"
#include "../core/ctrl.h"
#include "mastermeasure.h"


MasterConfigForm::MasterConfigForm(QWidget *parent)
	: QWidget(parent)
{
	ui_.setupUi(this);

	QStringList fil_options;
	AprCfg::getNamesByAttr(PM_FIL_ATTR, fil_options);
	ui_.filter_combo_->addItems(fil_options);

	// set font size
	QFont font = this->font();
	font.setPixelSize(font.pointSize() * 4 / 3);
	setFont(font);

	setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
	showMaximized();

	connect(ui_.item_combo_, SIGNAL(currentIndexChanged(const QString &)), 
			this, SLOT(MasterTypeChanged(const QString &)));
	connect(ui_.back_button_, SIGNAL(clicked()), this, SLOT(FormShouldReturn()));
	connect(ui_.start_button_, SIGNAL(clicked()), this, SLOT(StartMasterMeasure()));

	connect(ui_.dlm_cooltime_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
	connect(ui_.x0_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
	connect(ui_.y0_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
	connect(ui_.hue_cooltime_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
	connect(ui_.ref_wave_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
	connect(ui_.cooltime2_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
	connect(ui_.vl_cooltime_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
	connect(ui_.vol_limit_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
	connect(ui_.wait_time_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));


	connect(ui_.ref_bias1_button_, SIGNAL(clicked()), this, SLOT(EditMultiValue()));
	connect(ui_.ref_bias2_button_, SIGNAL(clicked()), this, SLOT(EditMultiValue()));
	connect(ui_.ref_vl1_button_, SIGNAL(clicked()), this, SLOT(EditMultiValue()));
	connect(ui_.ref_vl2_button_, SIGNAL(clicked()), this, SLOT(EditMultiValue()));

	ref_vl_range_code_   = PM_VL9_99u;
	ref_bias_range_code_ = PM_B99_9mA;
}

MasterConfigForm::~MasterConfigForm()
{
	delete ui_.item_combo_;
	delete ui_.back_button_;
	delete ui_.start_button_;
	delete ui_.dlm_cooltime_button_;
	delete ui_.dlm_ofst_check_;
	delete ui_.x0_button_;
	delete ui_.y0_button_;
	delete ui_.hue_cooltime_button_;
	delete ui_.hue_ofst_check_;
	delete ui_.ref_wave_button_;
	delete ui_.cooltime2_button_;
	delete ui_.filter_combo_;
	delete ui_.ref_bias1_button_;
	delete ui_.ref_bias2_button_;
	delete ui_.ref_vl1_button_;
	delete ui_.ref_vl2_button_;
	delete ui_.vl_cooltime_button_;
	delete ui_.vl_ofst_check_;
	delete ui_.vol_limit_button_;
	delete ui_.wait_time_button_;
	delete ui_.label1_;
	delete ui_.label2_;
	delete ui_.label3_;
	delete ui_.label4_;
	delete ui_.label5_;
	delete ui_.label6_;
	delete ui_.label7_;
	delete ui_.label8_;
	delete ui_.label9_;
	delete ui_.label10_;
	delete ui_.label11_;
	delete ui_.label12_;
	delete ui_.label13_;
	delete ui_.label14_;
	delete ui_.label15_;
	delete ui_.label16_;
	delete ui_.label17_;
	delete ui_.dlm_frame_;
	delete ui_.hue_frame_;
	delete ui_.vl_frame_;
}


bool MasterConfigForm::PrepareHueList()
{
	MeasProg* pMeasProg = MeasProg::getInstance();
	program_s* p = pMeasProg->getProgPtr();

	usechn_t usechn;
	chnitem_t chnitem;
	QStringList items_list;
	bool found = AprCfg::getHueItemsList(*p, items_list, citem_, usechn, chnitem);
	if(found){
		ui_.item_combo_->clear();
		ui_.item_combo_->addItems(items_list);
	}
	return found;
}


void MasterConfigForm::MasterTypeChanged(const QString &item_info)
{
	if(item_info.contains(tr("HUE")) || item_info.contains(tr("POLH"))){
		calb_no_ = 0;
	}else if(item_info.contains(tr("VL"))){
		calb_no_ = 1;
	}else if(item_info.contains(tr("DLM"))){
		calb_no_ = 2;
	}
	chn_no_ = item_info.left(3).right(1).toInt();
	QFrame *frame[3] = { ui_.hue_frame_, ui_.vl_frame_, ui_.dlm_frame_ };
	for(int frame_index = 0; frame_index < 3; ++frame_index){
		frame[frame_index]->setVisible(frame_index == calb_no_);
	}

	// TODO:
	item_no_ = citem_[ui_.item_combo_->currentIndex()];
}


void MasterConfigForm::SaveConfigToProgram()
{
	MeasProg *meas_prog = MeasProg::getInstance();
	program_s *program = meas_prog->getProgPtr();

	program->itemno = item_no_;
	kind_s *kind = &program->kinds[0];
	if(0 == calb_no_){
		program->meastype = TYPE_HUEMASTER;
		kind->ofstcancel = ui_.hue_ofst_check_->isChecked() ? PM_ON : PM_OFF; 
		kind->cooldown = ui_.hue_cooltime_button_->text().toDouble();
		kind->hue_mlums[chn_no_-1] = ui_.ref_wave_button_->text().toDouble();	//TODO:
	}else if(1 == calb_no_){
		program->meastype = TYPE_VLMASTER1;
		kind->vl_cfg[chn_no_-1].brng = ref_bias_range_code_;
		kind->vl_cfg[chn_no_-1].mrng = ref_vl_range_code_;
		ushort filter_code = 0;
		AprCfg::getParamCodeByName(ui_.filter_combo_->currentText(), PM_FIL_ATTR, filter_code);
		kind->vl_cfg[chn_no_-1].filter = filter_code;
		kind->vl_cfg[chn_no_-1].waittime = ui_.wait_time_button_->text().toDouble();
		kind->vl_cfg[chn_no_-1].vlimit = ui_.vol_limit_button_->text().toDouble();

		QString value_unit = ui_.ref_bias1_button_->text();
		QString value = value_unit.left(value_unit.indexOf(tr(" ")));
		kind->vl_cfg[chn_no_-1].b_v1 = value.toDouble();

		value_unit = ui_.ref_bias2_button_->text();
		value = value_unit.left(value_unit.indexOf(tr(" ")));
		kind->vl_cfg[chn_no_-1].b_v2 = value.toDouble();
		
		value_unit = ui_.ref_vl1_button_->text();
		value = value_unit.left(value_unit.indexOf(tr(" ")));
		kind->vl_cfg[chn_no_-1].t_v1 = value.toDouble();

		value_unit = ui_.ref_vl2_button_->text();
		value = value_unit.left(value_unit.indexOf(tr(" ")));
		kind->vl_cfg[chn_no_-1].t_v1 = value.toDouble();
		
		kind->cool_v1 = ui_.vl_cooltime_button_->text().toInt();
		kind->cool_v2 = ui_.cooltime2_button_->text().toInt();

		kind->ofstcancel = ui_.vl_ofst_check_->isChecked() ? PM_ON : PM_OFF;
	}else if(2 == calb_no_){
		program->meastype = TYPE_DLMASTER;
		kind->dlm_cfg[chn_no_-1].x0 = ui_.x0_button_->text().toDouble();
		kind->dlm_cfg[chn_no_-1].y0 = ui_.y0_button_->text().toDouble();
		kind->cooldown = ui_.dlm_cooltime_button_->text().toInt();
		kind->ofstcancel = ui_.dlm_ofst_check_->isChecked() ? PM_ON : PM_OFF;
	}

	// TODO: we should decide if the settings need to be saved to the file
//	meas_prog->saveProgramToFile();
}



void MasterConfigForm::FormShouldReturn()
{
	delete this;
}



void MasterConfigForm::StartMasterMeasure()
{
	SaveConfigToProgram();

	MasterMeasure *master_meas_form = new MasterMeasure(ui_.item_combo_->currentText());
	master_meas_form->show();
}



void MasterConfigForm::EditSingleValue()
{
//	connect(ui_.dlm_cooltime_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
//	connect(ui_.x0_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
//	connect(ui_.y0_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
//	connect(ui_.hue_cooltime_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
//	connect(ui_.ref_wave_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
//	connect(ui_.cooltime2_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
//	connect(ui_.vl_cooltime_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
//	connect(ui_.vol_limit_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));
//	connect(ui_.wait_time_button_, SIGNAL(clicked()), this, SLOT(EditSingleValue()));

	QPushButton *button = qobject_cast<QPushButton *>(sender());
	ValueForm *value_form;
	const rng_tbl_s *range;
//	double ori_value = 0.0;
	if(button == ui_.dlm_cooltime_button_ || button == ui_.hue_cooltime_button_
			|| button == ui_.vl_cooltime_button_ || button == ui_.cooltime2_button_){
		range = AprCfg::getRangeByCode(PM_COOLT);
		
	}else if(button == ui_.x0_button_ || button == ui_.y0_button_){
		range = AprCfg::getRangeByCode(PM_DLMXY);
	}else if(button == ui_.ref_wave_button_){
		range = AprCfg::getRangeByCode(PM_M770nm);
	}else if(button == ui_.vol_limit_button_){
		range = AprCfg::getRangeByCode(PM_L9_99V);
	}else if(button == ui_.wait_time_button_){
		range = AprCfg::getRangeByCode(PM_T9999ms);
	}

	value_form = new ValueForm(button->text().toDouble(), range, this);
	if(value_form->exec() == QDialog::Accepted){
		button->setText(value_form->getValueString());
	}
	delete value_form;
}



void MasterConfigForm::EditMultiValue()
{
	QPushButton *button = qobject_cast<QPushButton *>(sender());
	ValueSettingsForm *value_settings_dlg;
	int master_kind = 0;
	QString current_range;
	QString lower_value, upper_value;
	para_attr_e attr;

	if(button == ui_.ref_vl1_button_ || button == ui_.ref_vl2_button_){
		master_kind = 1;
		AprCfg::getParamNameByCode(ref_vl_range_code_, current_range);
		lower_value = ui_.ref_vl1_button_->text();
		lower_value = lower_value.left(lower_value.indexOf(" "));
		upper_value = ui_.ref_vl2_button_->text();
		upper_value = upper_value.left(upper_value.indexOf(" "));
		attr = PM_MRANGE_ATTR;
	}else{
		master_kind = 2;
		AprCfg::getParamNameByCode(ref_bias_range_code_, current_range);
		lower_value = ui_.ref_bias1_button_->text();
		lower_value = lower_value.left(lower_value.indexOf(" "));
		upper_value = ui_.ref_bias2_button_->text();
		upper_value = upper_value.left(upper_value.indexOf(" "));
		attr = PM_BRANGE_ATTR;
	}
	QString item_name = ui_.item_combo_->currentText();
//	item_name = item_name.left(item_name.indexOf("("));
	item_name = item_name.right(item_name.length() - item_name.indexOf("(") - 1);
	item_name = item_name.left(item_name.indexOf("_"));
	qDebug() << "item name =========== " << item_name;
	value_settings_dlg = new ValueSettingsForm(item_name, current_range, attr, lower_value, upper_value, master_kind);

	ushort range_code;
	if(value_settings_dlg->exec() == QDialog::Accepted){
		if(button == ui_.ref_vl1_button_ || button == ui_.ref_vl2_button_){
			ui_.ref_vl1_button_->setText(value_settings_dlg->getLowerValue() + " " + value_settings_dlg->getUnitString());
			ui_.ref_vl2_button_->setText(value_settings_dlg->getUpperValue() + " " + value_settings_dlg->getUnitString());
			AprCfg::getParamCodeByName(value_settings_dlg->getRangeString(), PM_MRANGE_ATTR, range_code);
			ref_vl_range_code_ = range_code;
		}else{
			ui_.ref_bias1_button_->setText(value_settings_dlg->getLowerValue() + " " + value_settings_dlg->getUnitString());
			ui_.ref_bias2_button_->setText(value_settings_dlg->getUpperValue() + " " + value_settings_dlg->getUnitString());
			AprCfg::getParamCodeByName(value_settings_dlg->getRangeString(), PM_BRANGE_ATTR, range_code);
			ref_bias_range_code_ = range_code;
		}
	}
	delete value_settings_dlg;
}
